<template>
  <div class="in-block">
    <div class="title">配置规则</div>
    <div class="btn-wk">
      <el-button v-for="(i,index) in wayData" :key="index" :class="[formData.analyseType==i.analyseType?'active-btn':'','custom-btn',$route.query.id?'btn-drop':'']" @click="!$route.query.id?hancdleChangeWay(i.analyseType):null">{{ i.name }}</el-button>
    </div>
    <el-form
      ref="elForm"
      class="dia-form"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="100px"
    >
      <el-col :span="18">
        <el-form-item label="选择事件" prop="ruleEntity.metricsCode">
          <el-select
            v-model="formData.ruleEntity.metricsCode"
            placeholder="请选择"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="item in formData.ruleEntity.dictList.metricsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label prop>
          <!-- 触发分析组件 -->
          <RealTimeAnalysis
            v-if="analyseType == 'stream'"
            ref="streamRef"
            :form="formData.rule"
            @exportObj="exportObj"
          />
          <!-- 聚合分析组件 -->
          <BatchAnalysis
            v-if="analyseType == 'batch'"
            ref="batchRef"
            :form="formData.rule"
            @exportObj="exportObj"
          />
          <!-- 时序分析组件 -->
          <RuleLabel
            v-if="analyseType == 'cep'"
            ref="cepRef"
            :form="formData.rule"
            @exportObj="exportObj"
          />
        </el-form-item>
      </el-col>

      <el-col :span="18">
        <el-form-item label=" 事件描述" prop="eventDetail.description">
          <Tribute
            ref="tribute"
            :behavior_field_data="formData.rule.transferArray.behavior_field_data"
            :entity_field="formData.rule.transferArray.attrList"
            :value="formData.eventDetail.description"
            :entity_type="formData.rule.transferArray.entity_type"
            @input="showDesc"
          />

          <!-- <div class="checkButton" @click="getDescriptionExample">
            <i class="el-icon-view">预览</i>
            输入"#"可插入参数
          </div> -->

          <!-- <el-input
            ref="textarea"
            v-model="formData.eventDetail.temDesc"
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 10 }"
            disabled
            class="textareaC"
          /> -->
        </el-form-item>
      </el-col>
    </el-form>
  </div>
</template>
<script>
import RealTimeAnalysis from '@/components/Common/realTimeAnalysis.vue'
import BatchAnalysis from '@/components/Common/batchAnalysis.vue'
import RuleLabel from '@/components/Common/ruleLabel.vue'

import {
  getDescriptionExample
} from '@/api/icp/labelClassDisplay'
import Tribute from '@/components/Common/Tribute.vue'
export default {
  components: { RealTimeAnalysis, BatchAnalysis, RuleLabel, Tribute },
  props: {
    // 表单
    form: {
      type: Object,
      default: () => {}
    },
    // 类型
    analyseType: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      rules: {
        'eventDetail.name': [{ required: true, message: '不能为空', trigger: 'blur' }],
        'ruleEntity.metricsCode': [{ required: true, message: '不能为空', trigger: 'change' }],
        'eventDetail.description': [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      desc: '',
      wayData: [
        { name: '触发分析', analyseType: 'stream' }, // 实时分析
        { name: '聚合分析', analyseType: 'batch' }, // 批量分析
        { name: '时序分析', analyseType: 'cep' } // 序列分析
      ]
    }
  },
  computed: {
    formData: {
      get() {
        return this.form
      },
      set() {}
    }
  },
  watch: {
    formData: {
      handler(newsVal) {},
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },

  methods: {
    // // 获取规则部分列表
    // getRuleTables(data) {
    //   // 获取属性第一个列表
    //   queryByEntityId(data).then(res => {
    //     this.formData.rule.transferArray.attrList = res.map(item => {
    //       return {
    //         name: item.field,
    //         value: item.name,
    //         label: item.field,
    //         propId: item.propId,
    //         type: item.type,
    //         isEnum: item.isEnum,
    //         enumList: item.enumList
    //       }
    //     })
    //   })

    //   // 获取实体行为的行为列表
    //   behaviorByEntityId(data).then(res => {
    //     this.formData.rule.transferArray.actionList = res.map(item => {
    //       return {
    //         value: item.acode,
    //         label: item.behaviorDesc,
    //         behaviorFields: item.behaviorFields
    //       }
    //     })
    //   })
    // },
    //  切换规则分析方式
    hancdleChangeWay(field) {
      this.formData.analyseType = field
      if (field == 'batch') {
        if (!this.$route.query.id) {
          this.formData.rule.transferArray.groupList = [{ list: [] }]
        }
        this.formData.rule.action.conditions.forEach(item => {
          item.attr = item.attr ? item.attr : [{}]
        })
      } else if (field == 'cep') {
        this.formData.rule.action.attr = [{}]
      }
      this.$emit('handleWay', this.formData.analyseType)
    },
    showDesc(data) {
      this.formData.eventDetail.description = data.value
      // this.formData.eventDetail.tempHtml= data.html
      this.formData.eventDetail.temDesc = ''

      this.$refs.textarea?.resizeTextarea()
    },
    // 获取传输来的值
    exportObj(data) {
      this.formData.rule.transferArray.behavior_field_data = data.behavior_field_data
    },
    // 确保下拉框能选择
    sure() {
      // this.$forceUpdate()
    },

    async submit() {
      if (!this.formData.eventDetail.description) {
        $('.descriptionDiv').css('border', '1px solid #ff4949')
      }
      const refLabel = this.analyseType + 'Ref'
      const res = await this.$refs.elForm.validate()
      $('.descriptionDiv').css('border', '1px solid #dcdfe6')
      const res1 = await this.$refs[refLabel].submitLabel()
      if (!res1) {
        return
      } else {
        if (this.formData.rule.action.conditions.length === 0) {
          this.$message.warning('实体行为不能为空')
          return
        }
        if (!this.formData.eventDetail.description) {
          this.$message.warning('请输入事件描述')
          return
        }
        // if (this.formData.eventDetail.description && !this.formData.eventDetail.temDesc) {
        //   this.$message.warning('请先点击预览')
        //   return
        // }
        this.formData.priority = this.formData.eventDetail.priority
        this.formData = Object.assign({}, this.formData)

        return new Promise((resolve, reject) => {
          console.log(this.formData)
          resolve(this.formData)
        })
      }
    },
    getDescriptionExample() {
      if (this.formData.eventDetail.description.length >= 500) {
        this.$message.warning('不得超过500个字符')
        return
      }
      getDescriptionExample(this.formData.eventDetail.description).then(res => {
        this.formData.eventDetail.temDesc = res
        // eslint-disable-next-line no-undef
        $("textarea[disabled='disabled']").text(res)
        this.$refs.textarea.resizeTextarea()
      })
    }

  }
}
</script>
<style scoped lang="scss">
.btn-drop{
  cursor:no-drop;
}
.btn-wk{
  width:360PX;
  margin-bottom:20px;
}
.dia-form {
  display: flex;
  flex-direction: column;
}
.ltips {
  font-weight: 500;
  margin-left: 11px;
  background-color: #f0f0f0;
  color: #94989c;
  font-size: 14px;
  padding: 0 2px;
}
.checkButton i {
  margin: 10px 10px 10px 0;
  text-align: center;
  line-height: 32px;
  width: 78px;
  height: 32px;
  display: inline-block;
  background: rgba(206, 224, 255, 0.53);
  border: 1px solid #2979ff;
  border-radius: 4px;
  cursor: pointer;
}
.el-col + .el-col {
  margin-top: 20px;
}
::v-deep{
  .el-icon-close{
    cursor: pointer;
  }
}
</style>
